package fi.foyt.cs.controller;

import java.util.List;

import fi.foyt.cs.persistence.dao.oauth.AuthenticatedUserCodeDAO;
import fi.foyt.cs.persistence.dao.oauth.AuthenticatedUserTokenDAO;
import fi.foyt.cs.persistence.dao.oauth.AuthorizedClientDAO;
import fi.foyt.cs.persistence.dao.oauth.AuthenticatedUserDAO;
import fi.foyt.cs.persistence.domainmodel.oauth.AuthenticatedUserToken;
import fi.foyt.cs.persistence.domainmodel.oauth.AuthorizedClient;
import fi.foyt.cs.persistence.domainmodel.oauth.AuthenticatedUser;
import fi.foyt.cs.persistence.domainmodel.oauth.AuthenticatedUserCode;

public class OAuthController {

  public AuthorizedClient createAuthorizedClient(String clientId, String clientSecret, String returnUrl, String clientName, Boolean autoAcceptScopes) {
    AuthorizedClientDAO authorizedClientDAO = new AuthorizedClientDAO();
    return authorizedClientDAO.create(clientId, clientSecret, returnUrl, clientName, autoAcceptScopes);
  }
  
  public AuthorizedClient findAuthorizedClientByClientIdAndClientSecret(String clientId, String clientSecret) {
    AuthorizedClientDAO authorizedClientDAO = new AuthorizedClientDAO();
    return authorizedClientDAO.findByClientIdAndClientSecret(clientId, clientSecret);
  }
  
  public AuthorizedClient findAuthorizedClientByClientId(String clientId) {
    AuthorizedClientDAO authorizedClientDAO = new AuthorizedClientDAO();
    return authorizedClientDAO.findByClientId(clientId);
  }
  
  public List<AuthorizedClient> listAuthorizedClients() {
    AuthorizedClientDAO authorizedClientDAO = new AuthorizedClientDAO();
    return authorizedClientDAO.listAll();
  }
  
  public AuthorizedClient updateAuthorizedClientReturnUrl(AuthorizedClient authorizedClient, String returnUrl) {
    AuthorizedClientDAO authorizedClientDAO = new AuthorizedClientDAO();
    return authorizedClientDAO.updateReturnUrl(authorizedClient, returnUrl);
  }
  
  public AuthorizedClient updateAuthorizedClientClientName(AuthorizedClient authorizedClient, String clientName) {
    AuthorizedClientDAO authorizedClientDAO = new AuthorizedClientDAO();
    return authorizedClientDAO.updateClientName(authorizedClient, clientName);
  }
  
  public AuthorizedClient updateAuthorizedClientAutoAcceptScopes(AuthorizedClient authorizedClient, Boolean autoAcceptScopes) {
    AuthorizedClientDAO authorizedClientDAO = new AuthorizedClientDAO();
    return authorizedClientDAO.updateAutoAcceptScopes(authorizedClient, autoAcceptScopes);
  }
  
  public void deleteAuthrorizedClient(AuthorizedClient authorizedClient) {
    AuthorizedClientDAO authorizedClientDAO = new AuthorizedClientDAO();
    authorizedClientDAO.delete(authorizedClient);
  }
  
  public AuthenticatedUser createAuthenticatedUser(String userId, String grantedScopes) {
    AuthenticatedUserDAO authenticatedUserDAO = new AuthenticatedUserDAO();
    return authenticatedUserDAO.create(userId, grantedScopes);
  }
 
  public AuthenticatedUser findAuthenticatedUserById(Long id) {
    AuthenticatedUserDAO authenticatedUserDAO = new AuthenticatedUserDAO();
    return authenticatedUserDAO.findById(id);
  }
  
  public AuthenticatedUser findAuthenticatedUserByUserId(String userId) {
    AuthenticatedUserDAO authenticatedUserDAO = new AuthenticatedUserDAO();
    return authenticatedUserDAO.findByUserId(userId);
  }
  
  public AuthenticatedUser updateAuthenticatedUserGrantedScopes(AuthenticatedUser authenticatedUser, String grantedScopes) {
    AuthenticatedUserDAO authenticatedUserDAO = new AuthenticatedUserDAO();
    return authenticatedUserDAO.updateGrantedScopes(authenticatedUser, grantedScopes);
  }
  
  public void deleteAuthenticatedUser(AuthenticatedUser authenticatedUser) {
    AuthenticatedUserDAO authenticatedUserDAO = new AuthenticatedUserDAO();
    authenticatedUserDAO.delete(authenticatedUser);
  }
  
  public AuthenticatedUserCode createAuthenticatedUserCode(AuthenticatedUser authenticatedUser, String code) {
    AuthenticatedUserCodeDAO authenticatedUserCodeDAO = new AuthenticatedUserCodeDAO();
    return authenticatedUserCodeDAO.create(authenticatedUser.getId().getId(), code);
  }
  
  public AuthenticatedUserCode findAuthenticatedUserCodeByAuthenticatedUser(AuthenticatedUser authenticatedUser) {
    AuthenticatedUserCodeDAO authenticatedUserCodeDAO = new AuthenticatedUserCodeDAO();
    return authenticatedUserCodeDAO.findByAuthenticatedUserId(authenticatedUser.getId().getId());
  }
  
  public AuthenticatedUserCode findAuthenticatedUserCodeByCode(String code) {
    AuthenticatedUserCodeDAO authenticatedUserCodeDAO = new AuthenticatedUserCodeDAO();
    return authenticatedUserCodeDAO.findByCode(code);
  }
  
  public AuthenticatedUserCode findAuthenticatedUserCodeByAuthenticatedUserAndCode(AuthenticatedUser authenticatedUser, String code) {
    AuthenticatedUserCodeDAO authenticatedUserCodeDAO = new AuthenticatedUserCodeDAO();
    return authenticatedUserCodeDAO.findByAuthenticatedUserIdAndCode(authenticatedUser.getId().getId(), code);
  }
  
  public void deleteAuthenticatedUserCode(AuthenticatedUserCode authenticatedUserCode) {
    AuthenticatedUserCodeDAO authenticatedUserCodeDAO = new AuthenticatedUserCodeDAO();
    authenticatedUserCodeDAO.delete(authenticatedUserCode);
  }
  
  public AuthenticatedUserToken createAuthenticatedUserToken(AuthenticatedUser authenticatedUser, String token) {
    AuthenticatedUserTokenDAO authenticatedUserTokenDAO = new AuthenticatedUserTokenDAO();
    return authenticatedUserTokenDAO.create(authenticatedUser.getId().getId(), token);
  }
  
  public AuthenticatedUserToken findAuthenticatedUserTokenByToken(String token) {
    AuthenticatedUserTokenDAO authenticatedUserTokenDAO = new AuthenticatedUserTokenDAO();
    return authenticatedUserTokenDAO.findByToken(token);
  }
  
  public List<AuthenticatedUserToken> listAuthenticatedUserTokensByAuthenticatedUser(AuthenticatedUser authenticatedUser) {
    AuthenticatedUserTokenDAO authenticatedUserTokenDAO = new AuthenticatedUserTokenDAO();
    return authenticatedUserTokenDAO.listByAuthenticatedUserId(authenticatedUser.getId().getId());
  }
  
  public void deleteAuthenticatedUserToken(AuthenticatedUserToken authenticatedUserToken) {
    AuthenticatedUserTokenDAO authenticatedUserTokenDAO = new AuthenticatedUserTokenDAO();
    authenticatedUserTokenDAO.delete(authenticatedUserToken);
  }
}
